package hyit.app.dao.proxy;

import java.util.List;

import hyit.app.dao.ISecondaryAccountDAO;
import hyit.app.dao.impl.SecondaryAccountDAOImpl;
import hyit.app.dbc.DatabaseConnection;
import hyit.app.model.SecondaryAccount;

public class SecondaryAccountDAOProxy implements ISecondaryAccountDAO {
	private DatabaseConnection dbc = null;
	private ISecondaryAccountDAO dao = null;

	public SecondaryAccountDAOProxy() throws Exception {
		this.dbc = new DatabaseConnection();
		this.dao = new SecondaryAccountDAOImpl(this.dbc.getConnection());
	}

	@Override
	public boolean doCreate(SecondaryAccount sa) throws Exception {
		boolean flag = false;
		try {
			if (this.dao.checkAccount(sa.getAccount()) == null) {
				flag = this.dao.doCreate(sa);
			}
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return flag;
	}

	@Override
	public boolean update(SecondaryAccount sa) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public SecondaryAccount delete(SecondaryAccount sa) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SecondaryAccount checkAccount(String account) throws Exception {
		SecondaryAccount sa = null;
		try {
			sa = this.dao.checkAccount(account);
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return sa;
	}

	@Override
	public SecondaryAccount validate(String account, String password)
			throws Exception {
		SecondaryAccount sa = null;
		try {
			sa = this.dao.validate(account, password);
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return sa;
	}

	@Override
	public List<SecondaryAccount> getByKeyID(Integer keyID) throws Exception {
		List<SecondaryAccount> all = null;
		try {
			all = this.dao.getByKeyID(keyID);
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return all;
	}

	@Override
	public SecondaryAccount validateByCard(String cardMac) throws Exception {
		SecondaryAccount sa = null;
		try {
			sa = this.dao.validateByCard(cardMac);
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return sa;
	}

}
